package in.muton.edushare.components.autocomplete.api;

import in.muton.edushare.wicket.models.CayenneList;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.GenericAudience;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public abstract class NewAudienceHelper implements Serializable{
	private static final long serialVersionUID = -6832308538538590733L;
	public abstract List<String> getStringList();
	public abstract int getRoleId(String s);
	
	public List<String> getOptions(String s){
		List<String> retList = new ArrayList<String>();
		List<String> sList= getStringList();
		for(String sL:sList){
			if(sL.toLowerCase().contains(s.toLowerCase())){
				retList.add(sL);
			}
		}
		return retList;
	}

	public abstract CayenneList<Users> getAllUsers(String s);

	public abstract CayenneList<GenericAudience> getAudiences(String s);

	public abstract String getNameForAudience(String s);

	public static List<Universities> getUnivList(ORMInterface orm) {
		return new UniversitiesDAO(orm).getAllUniversities();
	}

	public static List<String> breakString(String s) {
		List<String> output = new ArrayList<String>();
		// First break by << and >>
		MainInviter.log.debug("Input string is "+s);
		Pattern p = Pattern.compile("\\[.+?\\];");
		Matcher m = p.matcher(s);
		while (m.find()) {
			String extract = s.substring(m.start(), m.end());
			MainInviter.log.debug("Remaining string is "+s);
			output.add(extract);
			MainInviter.log.debug("Extracted string is "+extract);
			if (m.start() == 0) {
				s = s.substring(m.end(), s.length());
			} else {
				s = s.substring(0, m.start())
						+ s.substring(m.end(), s.length());
			}
			if(s.length() == 0)
				break;
			m = p.matcher(s);
		}
		Pattern p2 = Pattern.compile(";+");
		s = s.replace(' ', ';').replace(',', ';').replace('\n', ';').replace(
				'\r', ';');
		Matcher m2 = p2.matcher(s);
		String[] arr = m2.replaceAll(";").split(";");
		for (String a : arr) {
			if(StringUtil.isValid(a)){
				output.add(a);
				MainInviter.log.debug("User string is "+a);
			}
		}
		return output;
	}
}
